//********************************************************************
//	created:	20:5:2013   12:53
//	filename: 	synchelper.cpp
//	author:		tiamo	
//	purpose:	sync helper
//********************************************************************

#include "stdafx.h"
#include "synchelper.h"

namespace utils
{
	//
	// signal
	//
	void Event::signal()
	{
		{
			std::unique_lock<std::mutex> locker(mMutex);
			mSignalState													= true;
		}
		mConditionVar.notify_all();
	}

	//
	// wait
	//
	bool Event::wait(uint32_t timeout)
	{
		std::unique_lock<std::mutex> locker(mMutex);
		if(!mSignalState)
		{
			if(timeout == 0xffffffff)
				mConditionVar.wait(locker, [this](){return mSignalState;});
			else
				mConditionVar.wait_for(locker, std::chrono::milliseconds(timeout), [this](){return mSignalState;});
		}
		
		return mSignalState;
	}

	//
	// start
	//
	void Thread::start()
	{
		mThread																= new std::thread(&Thread::run, this);
	}

	//
	// wait
	//
	void Thread::wait()
	{
		if(mThread)
			mThread->join();
	}
}
